package com.example.carrierapp.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
    /**
     * 2018-07-09T15:40:11  -> 2018.07.09 15:40
     *
     * @param oldtime
     * @return
     */
    public static String creatYearMinuteTime(String oldtime) {
        if (TextUtils.isEmpty(oldtime)) {
            return "";
        }
        String time = "";
        String mm = "";
        time = (oldtime.split("T")[0] + " ").replace("-", ".");
        if (oldtime.split("T").length > 1) {
            if (oldtime.split("T")[1].split(":").length > 1) {
                mm = oldtime.split("T")[1].split(":")[0] + ":" + oldtime.split("T")[1].split(":")[1];
            } else {
                mm = oldtime.split("T")[1].split(":")[0];
            }
        }
        return time + mm;
    }

    /**
     * 2018-07-09T15:40:11  -> 07.09
     */
    public static String creatMonthDay(String oldtime) {
        if (TextUtils.isEmpty(oldtime)) {
            return "";
        }
        String time = "";
        time = (oldtime.split("T")[0]).replace("-", "-");//2018-07-09

        String[] times = time.split("-");
        if (times.length == 3) {
            time = times[1] + "-" + times[2];
        }

        return time;
    }


    /**
     * 2018-07-09T15:40:11  -> 2018.07.09
     */
    public static String creatDay(String oldtime) {
        if (TextUtils.isEmpty(oldtime)) {
            return "";
        }
        String time = "";
        time = (oldtime.split("T")[0]).replace("-", "/");
        return time;
    }


    /**
     * 2021-09-11 13:24:00 -->
     */
    public static long date2Mills(String date) {
        long mills = 0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            mills = simpleDateFormat.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            mills = 0;
        }
        return mills;
    }

    /**
     * 获得口头时间字符串，如今天，昨天等
     *
     * @param d 时间格式为yyyy-MM-dd HH:mm:ss
     * @return 口头时间字符串
     */
    public static String getTimeInterval(String d) {
        Date date = formatStringByFormat(creatYearMinuteTime(d), "yyyy.MM.dd HH:mm");
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        int nowYear = now.get(Calendar.YEAR);
        int nowMonth = now.get(Calendar.MONTH);
        int nowWeek = now.get(Calendar.WEEK_OF_MONTH);
        int nowDay = now.get(Calendar.DAY_OF_WEEK);
        int nowHour = now.get(Calendar.HOUR_OF_DAY);
        int nowMinute = now.get(Calendar.MINUTE);

        Calendar ca = Calendar.getInstance();
        if (date != null)
            ca.setTime(date);
        else
            ca.setTime(new Date());
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH);
        int week = ca.get(Calendar.WEEK_OF_MONTH);
        int day = ca.get(Calendar.DAY_OF_WEEK);
        int hour = ca.get(Calendar.HOUR_OF_DAY);
        int minute = ca.get(Calendar.MINUTE);
        if (year != nowYear) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
            //不同年份
            return sdf.format(date);
        } else {
            if (month != nowMonth) {
                //不同月份
                SimpleDateFormat sdf = new SimpleDateFormat("M月dd日", Locale.CHINA);
                return sdf.format(date);
            } else {
                if (week != nowWeek) {
                    //不同周
                    SimpleDateFormat sdf = new SimpleDateFormat("M月dd日", Locale.CHINA);
                    return sdf.format(date);
                } else if (day != nowDay) {
                    if (day + 1 == nowDay) {
                        return "昨天" + formatDateByFormat(date, "HH:mm");
                    }
                    if (day + 2 == nowDay) {
                        return "前天" + formatDateByFormat(date, "HH:mm");
                    }
                    //不同天
                    SimpleDateFormat sdf = new SimpleDateFormat("M月dd日", Locale.CHINA);
                    return sdf.format(date);
                } else {
                    //同一天
                    int hourGap = nowHour - hour;
                    if (hourGap == 0)//1小时内
                    {
                        if (nowMinute - minute < 1) {
                            return "刚刚";
                        } else {
                            return (nowMinute - minute) + "分钟前";
                        }
                    } else if (hourGap >= 1 && hourGap <= 12) {
                        return hourGap + "小时前";
                    } else {
                        SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm", Locale.CHINA);
                        return sdf.format(date);
                    }
                }
            }
        }
    }

    /**
     * 获得口头时间字符串，如今天，昨天等
     *
     * @param d 时间格式为yyyy-MM-dd HH:mm:ss
     * @return 口头时间字符串
     */
    public static String getNewTimeInterval(String d) {
        Date date = formatStringByFormat(d, "yyyy.MM.dd HH:mm:ss");
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        int nowYear = now.get(Calendar.YEAR);
        int nowMonth = now.get(Calendar.MONTH);
        int nowWeek = now.get(Calendar.WEEK_OF_MONTH);
        int nowDay = now.get(Calendar.DAY_OF_WEEK);
        int nowHour = now.get(Calendar.HOUR_OF_DAY);
        int nowMinute = now.get(Calendar.MINUTE);

        Calendar ca = Calendar.getInstance();
        if (date != null)
            ca.setTime(date);
        else
            ca.setTime(new Date());
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH);
        int week = ca.get(Calendar.WEEK_OF_MONTH);
        int day = ca.get(Calendar.DAY_OF_WEEK);
        int hour = ca.get(Calendar.HOUR_OF_DAY);
        int minute = ca.get(Calendar.MINUTE);
        if (year != nowYear) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
            //不同年份
            return "三个月前";
        } else {
            if (Math.abs(month - nowMonth) >= 3) {
                //不同月份
                return "三个月前";
            } else if (Math.abs(month - nowMonth) < 3 && Math.abs(month - nowMonth) >= 1) {
                return "一个月前";
            } else if (Math.abs(month - nowMonth) < 1) {
                //不同周
                if (week != nowWeek) {
                    return "一周前";
                } else if (day != nowDay) {
                    if (day + 1 == nowDay) {
                        return "昨天" + formatDateByFormat(date, "HH:mm");
                    }
                    if (day + 2 == nowDay) {
                        return "前天" + formatDateByFormat(date, "HH:mm");
                    } else {
                        return Math.abs((day - nowDay)) + "天前";
                    }
                } else {
                    //同一天
                    int hourGap = nowHour - hour;
                    //1小时内
                    if (hourGap == 0) {
                        if (nowMinute - minute < 1) {
                            return "刚刚";
                        } else {
                            return (nowMinute - minute) + "分钟前";
                        }
                    } else if (hourGap >= 1 && hourGap <= 24) {
                        return hourGap + "小时前";
                    }
                }
            }
        }
        return "";
    }

    /**
     * 功能描述：以指定的格式来格式化日期
     *
     * @param date   Date 日期
     * @param format String 格式
     * @return String 日期字符串
     */
    public static String formatDateByFormat(Date date, String format) {
        String result = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
                result = sdf.format(date);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 日期字符串转换为日期
     *
     * @param date    日期字符串
     * @param pattern 格式
     * @return 日期
     */
    public static Date formatStringByFormat(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.CHINA);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String secondsToString(Long second) {
        String result = "";
        int h = 60 * 60;
        int m = 60;

        Long hs = second / h;
        result += hs > 9 ? hs.toString() + ":" : "0" + hs.toString() + ":";
        Long ss = second % h;
        Long ms = ss / m;
        result += ms > 9 ? ms.toString() + ":" : "0" + ms.toString() + ":";
        Long sss = ss % m;
        result += sss > 9 ? sss.toString() : "0" + sss.toString();
        return result;
    }
}
